#!/bin/sh

echo ''
echo 'SNiFF+ Installation Checker 3.2'
echo ''

PWD=${PWD-`pwd`}

ERR_NO_EXEC="no execute permission"
ERR_NO_SEARCH="no search permission"
ERR_NO_READ="no read permission"
ERR_NOT_DIR="not a directory"
ERR_NO_FILE="file does not exist"
ERR_NO_DIR="directory does not exist"
ERR_NO_LINK="symbolic link does not exist"

WARNING='*** '

ECHO=`echo "\c"`
if [ "$ECHO" = "" ]; then
  ECHO=echo
  ECHO_N="\c"
else
  ECHO="echo -n"
  ECHO_N=
fi

# Determine $SNIFF_DIR
if [ "$1" ]; then
  SNIFF_DIR=$1
else 
  DEFAULT_SNIFF_DIR=${SNIFF_DIR-""}
  while [ "1" ]; do
	$ECHO 'Enter location of SNiFF+ ? ['$DEFAULT_SNIFF_DIR'] > '$ECHO_N
	read ANSWER
	if [ "$ANSWER" ]; then
	  SNIFF_DIR=$ANSWER
	else
	  SNIFF_DIR=$DEFAULT_SNIFF_DIR
	fi
	if [ "$SNIFF_DIR" ]; then
	  E="`cd $SNIFF_DIR 2>&1 | sed -e 's/^.*:[ 	]*\(.*\)/\1/'`"
	  if [ "$E" ]; then
		echo ''
		echo "$SNIFF_DIR: $E"
		echo ''
	  else
		break
	  fi
	fi
  done
fi

cd $SNIFF_DIR
SNIFF_DIR=`pwd`

echo ''
echo Checking installation in $SNIFF_DIR

echo ''
if [ -f Version ]; then
  if [ -r Version ]; then
    SNIFF_VERSION=`cat Version 2>/dev/null`
    echo "Version: $SNIFF_VERSION"
  else
    echo "${WARNING}Version: $ERR_NO_READ"
  fi
else
  echo "${WARNING}Version: $ERR_NO_FILE"
fi

ROOT_DIR=
ROOT_FILES="
SitePrefs.sniff
Version
license.dat"

BIN_DIR=bin
BIN_FILES="
Convert.ClearCase.sh
appcomm
convert2html
convert2mif
get_licenseinfo
htmlviewer
install.check
lmdown
lmgrd
lmhostid
lmremove
lmreread
lmstat
lmver
pygdb
sniff
sniffPrintTargetCommand.sh
sniffTouchTarget.sh
sniffUpdateFile.PVCS.sh
sniffUpdateFile.RCS.sh
sniffUpdateFile.SCCS.sh
sniff_arch
sniff_cvs_filter
sniff_fortran
sniff_fortran_fixed
sniff_genproj
sniff_to_softbench.sh
sniff_wrapper
sniffaccess
sniffapi
sniffappcomm
sniffemacs
sniffgdb_init3
sniffgdb_init4
sniffgdb_initDecdbx
sniffgdb_initIbmdbx
sniffgdb_initLadbx
sniffgdb_initSgidbx
sniffgdb_initXdb
sniffidl
sniffitcl
sniffjava
sniffjdb
sniffparser
sniffpython
softbench_to_sniff.sh
takefive
tclsh
unzip
"

CONFIG_DIR=config
CONFIG_FILES="
EmbSQL_Ignore
ErrorFormats
Filters
IV_Ignore
winapi_ignore
winapi_ignore_cpp
NIHCL_Ignore
RogueWave_Ignore
SiteMenus.sniff
blt.in
et_ignore
sniff-mode.el
tcltk.vars
template.C
template.cc
template.h
template.makefile
"

CONFIG_DOCU_DIR=config/docu
CONFIG_DOCU_FILES="
Class.dtmpl
Default.dtmpl
Enumeration.dtmpl
GlobalFunction.dtmpl
GlobalVariable.dtmpl
Header.dtmpl
InstanceVariable.dtmpl
Macro.dtmpl
Method.dtmpl
Separator.dtmpl
Struct.dtmpl
TypeDef.dtmpl
Union.dtmpl
mif.dtmpl
"

CONFIG_PARSER_DIR=config/parser
CONFIG_PARSER_FILES="
sniff_fortran.lsd
sniff_fortran_fixed.lsd
sniffidl.lsd
sniffitcl.lsd
sniffjava.lsd
sniffpython.lsd
template.lsd
"
 
CONFIG_ICONS_DIR=config/icons
CONFIG_ICONS_FILES="
 
"

LIB_DIR=lib
LIB_FILES=

LIB_PYTHON_DIR=lib/python
LIB_PYTHON_FILES="
"

LIB_PYTHON_SNIFF_DIR=lib/python/Sniff
LIB_PYTHON_SNIFF_FILES="
CmdNo.py
Editor.py
InstStat.py
Token.py
"

MAKE_SUPPORT_DIR=make_support
MAKE_SUPPORT_FILES="
Makefile.sample             mips-sgi-irix5.3.mk
alpha-dec-osf3.0.mk         mips-sni-sinix5.42.mk
general.c.mk                pa_risc-hp-hpux10.mk
general.fortran.mk          pa_risc-hp-hpux9.0.mk
general.idl.mk              rs6000-ibm-aix3.2.mk
rs6000-ibm-aix4.2.mk        general.java.mk
general.link.mk             general.mk
general.obsolete.mk         sniffPrintTargetCommand.sh
general.yl.mk               sniffTouchTarget.sh
sparc-sun-solaris2.3.mk     sparc-sun-sunos4.1.mk
i386-unknown-unixware7.mk
"


MAN_DIR=man
MAN_FILES=

MAN_MAN1_DIR=man/man1
MAN_MAN1_FILES="
lmdown.1
lmgrd.1
lmhostid.1
lmremove.1
lmreread.1
lmstat.1
lmver.1
"

POSTSCRIPT_DIR=postscript
POSTSCRIPT_FILES="
BitmapMask-3.0-0.ps
et-2.2-0.ps
smooth-2.2-0.ps
"
 

WS_SUPPORT_DIR=ws_support
WS_SUPPORT_FILES="
README
updateWS.sh
updateWScrisp.sh
"

WORKENV_DIR=workingenvs
WORKENV_FILES="
WorkingEnvData.sniff
"

VAR_LIST="
ROOT
BIN
CONFIG
CONFIG_DOCU
CONFIG_PARSER
VCS
ICONS
LIB
LIB_PYTHON
LIB_PYTHON_SNIFF
MAKE_SUPPORT
MAN
MAN_MAN1
POSTSCRIPT
WS_SUPPORT
WORKENV
"

echo ''
echo checking core directories and files

NEWLINE=
for V in $VAR_LIST ; do
  eval D=\$${V}_DIR
  if [ "$D" ]; then
	if [ "$NEWLINE" ]; then
	  echo ""
	  NEWLINE=
	fi
    echo checking directory $D
  fi
  E=
  if [ -n "$D" ]; then
    if [ -d $D ]; then
      if [ -r $D ]; then
		if [ ! -x $D ]; then
		  E=$ERR_NO_SEARCH
		fi
      else
		E=$ERR_NO_READ
      fi
    else
      if [ -f $D ]; then
		E=$ERR_NOT_DIR
      else
		E=$ERR_NO_DIR
      fi
    fi
  fi
  if [ -n "$E" ]; then
    echo "$WARNING$D: $E"
    NEWLINE=1
  else
    SLASH=/
    if [ -z "$D" ]; then
      SLASH=
    fi
    eval FILES=\$${V}_FILES
    for F in $FILES ; do
      E=
      F=$D$SLASH$F
      if [ -f $F ]; then
		if [ ! -r $F ]; then
		  E=$ERR_NO_READ
		fi
      else
		E=$ERR_NO_FILE
      fi
      if [ -n "$E" ]; then
		echo "$WARNING$F: $E"
		NEWLINE=1
      fi
    done
  fi
done

echo ''
echo checking installed architectures

ARCH_LIST=`ls -d bin/bin.* 2>/dev/null | sed -e 's#bin/bin\.##g' 2>/dev/null`

if [ -z "$ARCH_LIST" ]; then
  echo "${WARNING}no architectures installed"
else
  for A in $ARCH_LIST ; do
    echo "checking architecture $A"

    ARCH_BIN_FILES="
    appcomm
    sniff
    sniff_genproj
    sniffaccess
    sniffappcomm
    sniffemacs
    sniffparser
    "

      ARCH_LIC_FILES="lmdown lmgrd lmhostid lmremove lmreread lmstat lmver takefive"
      LIC_FILE=license.dat

    ARCH_BIN_FILES="$ARCH_BIN_FILES $ARCH_LIC_FILES"

    for B in $ARCH_BIN_FILES ; do
      E=
      F=bin/bin.$A/$B
      if [ -f $F ]; then
		if [ ! -r $F ]; then
		  E=$ERR_NO_EXEC
		fi
      else
		E=$ERR_NO_FILE
      fi
      if [ -n "$E" ]; then
		echo "$WARNING$F: $E"
      fi
    done

    for B in $ARCH_LIC_FILES ; do
      E=
      F=bin/$B
      if [ ! -f $F ]; then
		E=$ERR_NO_LINK
		echo "$WARNING$F: $E"
		echo "$WARNING$F: should be linked to sniff_wrapper"
      fi
    done
    F=$LIC_FILE
    if [ ! -f $F ]; then
      E=$ERR_NO_FILE
      echo "$WARNING$F: $E"
    fi

  done
fi

# --- examples

echo ''
echo 'checking example projects'
echo ''
if [ ! -d example ]; then
    echo "${WARNING}example projects are not installed"
else

    CXX_NAME="C++ example project"
    CXX_DIR="
example/c++
"
    CXX_DIRS="
example/c++/complex_dir
example/c++/complex_dir/team
example/c++/complex_dir/team/pwe
example/c++/complex_dir/team/rwe
example/c++/complex_dir/team/sowe
example/c++/complex_dir/team/sswe
example/c++/complex_dir/team/sswe/complex
example/c++/complex_dir/team/sswe/complex/complexlib
example/c++/complex_dir/team/sswe/complex/iolib
example/c++/complex_dir/team/working_envs_admin
example/c++/complex_dir/user
example/c++/complex_dir/user/complex
example/c++/complex_dir/user/complex/complexlib
example/c++/complex_dir/user/complex/iolib
example/c++/filebrowser
example/c++/filebrowser/docu
example/c++/filebrowser/docu/RCS
example/c++/filebrowser/RCS
example/c++/et3
example/c++/et3/CONTAINER
example/c++/et3/CONVERTERS
example/c++/et3/CONVERTERS/images
example/c++/et3/ENV
example/c++/et3/LOOKS
example/c++/et3/LOOKS/images
example/c++/et3/PRINTERS
example/c++/et3/PROGENV
example/c++/et3/PROGENV/images
example/c++/et3/STREAMFILTERS
example/c++/et3/UNIX
example/c++/et3/XSERVER
example/c++/et3/foundation
example/c++/et3/images
"
    
    C_NAME="C example project"
    C_DIR="
example/c
"
    C_DIRS="
example/c/pico_dir
example/c/pico_dir/team
example/c/pico_dir/team/pwe
example/c/pico_dir/team/rwe
example/c/pico_dir/team/sowe
example/c/pico_dir/team/sswe
example/c/pico_dir/team/working_envs_admin
example/c/pico_dir/user
"
    JAVA_NAME="Java example project"
    JAVA_DIR="
example/java
"
    JAVA_DIRS="
example/java/sniff_java
example/java/sniff_java/pwe_1
example/java/sniff_java/pwe_1/OfficeApp
example/java/sniff_java/pwe_1/OfficeApp/backoffice
example/java/sniff_java/pwe_1/OfficeApp/backoffice/BOgui
example/java/sniff_java/pwe_1/OfficeApp/backoffice/BOgui/BOimages
example/java/sniff_java/pwe_1/OfficeApp/backoffice/BOkernel
example/java/sniff_java/pwe_1/OfficeApp/backoffice/BOsimulation
example/java/sniff_java/pwe_1/OfficeApp/backoffice/BOtest
example/java/sniff_java/pwe_1/OfficeApp/backoffice/BOutil
example/java/sniff_java/pwe_1/OfficeApp/BOutilities
example/java/sniff_java/pwe_1/OfficeApp/BOutilities/BObrowser
example/java/sniff_java/pwe_1/OfficeApp/BOutilities/BOcanvas
example/java/sniff_java/pwe_2
example/java/sniff_java/pwe_2/OfficeApp
example/java/sniff_java/repository
example/java/sniff_java/working_envs_config
"
    F90_NAME="Fortran example project"
    F90_DIR="
example/fortran
"
    F90_DIRS="
example/fortran/commons
example/fortran/routines
example/fortran90
example/fortran90/HPF_Fortran_Library
"
    VAR_LIST="
    C
    CXX
    JAVA
    F90
"
  for V in $VAR_LIST ; do
    eval D=\$${V}_DIR
    eval DIRS=\$${V}_DIRS
    if [ "$D" ]; then
      if [ "$NEWLINE" ]; then
	   echo ""
	   NEWLINE=
      fi
	  NEWLINE=
	  eval N=\$${V}_NAME
	  echo checking $N
      if [ -d $D ]; then
	    for D in $DIRS; do
	      E=
	  	  if [ -n "$D" ]; then
		   if [ -d $D ]; then
		    if [ -r $D ]; then
			 if [ ! -x $D ]; then
			  E=$ERR_NO_SEARCH
			 fi
		    else
			 E=$ERR_NO_READ
		    fi
		   else
		    if [ -f $D ]; then
			 E=$ERR_NOT_DIR
		    else
			 E=$ERR_NO_DIR
		    fi
		   fi
	      fi  ### if [ -n "$D" ]
	      if [ -n "$E" ]; then
		   echo "$WARNING$D: $E"
		   NEWLINE=1
	      fi
	    done
      else
		echo "$WARNING$N is not installed."
		NEWLINE=1
      fi
    fi
    done
fi

echo ''
echo 'checking installed GNU software'
 
EXEC_PATH="`echo $PATH | sed -e 's/:/ /g'` /usr/local/bin"

echo ''
echo 'looking for GNU C/C++ compiler'
FOUND=
for G in g++ gcc ; do
  for D in $EXEC_PATH ; do
    if [ -r $D/$G ]; then
      PROG=$D/$G
      FOUND=yes
      VERSION=`$PROG -v 2>&1 | sed -n -e 's/gcc version //p'`
      echo "$PROG: GNU C/C++ compiler version $VERSION"
    fi
  done
done
if [ -z "$FOUND" ]; then
  echo "${WARNING}GNU C/C++ compiler is not installed"
fi

echo ''
echo 'looking for GNU debugger'
FOUND=
TMP_CMD=/tmp/sniff_$$
echo show version > $TMP_CMD
for D in $EXEC_PATH ; do
  if [ -r $D/gdb ]; then
    PROG=$D/gdb
    FOUND=yes
    VERSION=`$PROG -nx -batch -command=$TMP_CMD 2>&1 \
             | sed -n -e 's/^\(GDB [0-9][^,]*\),.*$/\1/p'`
    echo "$PROG: $VERSION"
  fi
done
rm -f $TMP_CMD
if [ -z "$FOUND" ]; then
  echo "${WARNING}GNU debugger is not installed"
fi

echo ''
echo 'looking for GNU make'
FOUND=
for G in gmake make ; do
  for D in $EXEC_PATH ; do
    if [ -r $D/$G ]; then
      PROG=$D/$G
      VERSION=`$PROG -v 2>&1`
	  VERSION=`echo "$VERSION" | sed -n -e 's/^\(GNU [mM]ake[^,]*\),.*$/\1/p'`
	  if [ -n "$VERSION" ]; then
		FOUND=yes
		echo "$PROG: $VERSION"
      fi
    fi
  done
done
if [ -z "$FOUND" ]; then
  echo "${WARNING}GNU make is not installed"
fi

echo ''
echo 'looking for RCS (Revision Control System)'
for D in $EXEC_PATH ; do
  FOUND=
  NOT_FOUND=
  for F in ci co ident merge rcs rcsdiff rcsmerge rlog ; do
    if [ -r $D/$F ]; then
      FOUND="$FOUND $F"
      PROG=$D/$F
      IDENT=$D/ident
      VERSION=`$IDENT $PROG 2>&1`
	  VERSION=`echo "$VERSION" | awk '/Id:/ { print $3; exit }' | sed -e 's/\..*$//'`
	  if [ -n "$VERSION" ]; then
		echo "$PROG: version $VERSION"
      fi
    else
      NOT_FOUND="$NOT_FOUND $F"
    fi
  done
  if [ -n "$FOUND" -a -n "$NOT_FOUND" ]; then
    echo "${WARNING}partial installation of RCS version $VERSION in $D"
    echo "${WARNING}missing:$NOT_FOUND"
  fi
done

echo ''
echo 'looking for GNU diff'
FOUND=
for G in gdiff diff ; do
  for D in $EXEC_PATH ; do
    if [ -r $D/$G ]; then
      PROG=$D/$G
      VERSION=`$PROG -v 2>&1`
	  VERSION=`echo "$VERSION" | sed -n -e 's/^.*\(version .*\)$/\1/p'`
	  if [ -n "$VERSION" ]; then
		FOUND=yes
		echo "$PROG: GNU diff $VERSION"
      fi
    fi
  done
done
if [ -z "$FOUND" ]; then
  echo "${WARNING}GNU diff is not installed"
fi

echo ''
echo 'looking for GNU diff3'
FOUND=
for G in gdiff3 diff3 ; do
  for D in $EXEC_PATH ; do
    if [ -r $D/$G ]; then
      PROG=$D/$G
      VERSION=`$PROG -v 2>&1`
	  VERSION=`echo "$VERSION" | sed -n -e 's/^.*\(version .*\)$/\1/p'`
	  if [ -n "$VERSION" ]; then
		FOUND=yes
		echo "$PROG: GNU diff3 $VERSION"
	  fi
    fi
  done
done
if [ -z "$FOUND" ]; then
  echo "${WARNING}GNU diff3 is not installed"
fi

echo ''
echo Installation check completed.
